<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author XPF
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'sports:sportsAverageScore:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ averageValueId: record.averageValueId })">
          {{ record.singleIndexAdaptGrade }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsSportsSportsAverageScoreList',
  });
</script>
<script lang="ts" setup>
  import {
    defineComponent
  } from 'vue';
  import {
    useI18n
  } from '/@/hooks/web/useI18n';
  import {
    useMessage
  } from '/@/hooks/web/useMessage';
  import {
    router
  } from '/@/router';
  import {
    Icon
  } from '/@/components/Icon';
  import {
    BasicTable,
    BasicColumn,
    useTable
  } from '/@/components/Table';
  import {
    sportsAverageScoreDelete,
    sportsAverageScoreListData
  } from '/@/api/sports/sportsAverageScore';
  import {
    useDrawer
  } from '/@/components/Drawer';
  import {
    FormProps
  } from '/@/components/Form';
  import InputForm from './form.vue';

  const {
    t
  } = useI18n('sports.sportsAverageScore');
  const {
    showMessage
  } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('单项平均分模块管理'),
  };

  const searchForm: FormProps = {
    baseColProps: {
      lg: 6,
      md: 8
    },
    labelWidth: 90,
    schemas: [{
        label: t('单项指标适配年级'),
        field: 'singleIndexAdaptGrade',
        component: 'Input',
      },
      {
        label: t('单项指标id'),
        field: 'indexId',
        component: 'Input',
      },
      {
        label: t('平均值适配性别'),
        field: 'averageValueAdaptSex',
        component: 'Input',
      },
      {
        label: t('行政级别'),
        field: 'adaptExecutiveLevel',
        component: 'RadioGroup',
        componentProps: {
          dictType: 's_index_area_level',
        },
      },
      {
        label: t('平均分值'),
        field: 'averageScore',
        component: 'Input',
      },
      {
        label: t('删除标记'),
        field: 'isDelete',
        component: 'RadioGroup',
        componentProps: {
          dictType: 's_index_delete',
        },
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'sys_search_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [{
      title: t('单项指标适配年级'),
      dataIndex: 'singleIndexAdaptGrade',
      key: 'a.single_index_adapt_grade',
      sorter: true,
      width: 230,
      align: 'left',
      slots: {
        customRender: 'firstColumn'
      },
    },
    {
      title: t('单项指标id'),
      dataIndex: 'indexId',
      key: 'a.index_id',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('平均值适配性别'),
      dataIndex: 'averageValueAdaptSex',
      key: 'a.average_value_adapt_sex',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('行政级别'),
      dataIndex: 'adaptExecutiveLevel',
      key: 'a.adapt_executive_level',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 's_index_area_level',
    },
    {
      title: t('平均分值'),
      dataIndex: 'averageScore',
      key: 'a.average_score',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('删除标记'),
      dataIndex: 'isDelete',
      key: 'a.is_delete',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 's_index_delete',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'sys_search_status',
    },
    {
      title: t('更新时间'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [{
        icon: 'clarity:note-edit-line',
        title: t('编辑单项平均分模块'),
        onClick: handleForm.bind(this, {
          averageValueId: record.averageValueId
        }),
        auth: 'sports:sportsAverageScore:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除单项平均分模块'),
        popConfirm: {
          title: t('是否确认删除单项平均分模块'),
          confirm: handleDelete.bind(this, {
            averageValueId: record.averageValueId
          }),
        },
        auth: 'sports:sportsAverageScore:edit',
      },
    ],
  };

  const [registerDrawer, {
    openDrawer
  }] = useDrawer();
  const [registerTable, {
    reload
  }] = useTable({
    api: sportsAverageScoreListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await sportsAverageScoreDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>
